/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Attendance.java
 *
 * Created on May 6, 2011, 1:41:20 AM
 */

package c0907m.group2.aitsm.gui;

import c0907m.group2.aitsm.core.db.*;
import c0907m.group2.aitsm.core.entity.*;
import c0907m.group2.aitsm.gui.component.DateTimeCellRenderer;
import c0907m.group2.aitsm.gui.component.DefaultPanel;
import c0907m.group2.aitsm.gui.component.StatusBar;
import c0907m.group2.aitsm.util.Helper;
import c0907m.group2.aitsm.util.Settings;
import com.standbysoft.component.date.swing.JDatePicker;
import com.standbysoft.component.date.swing.plaf.basic.SpinnerTimePickerUI;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author ISTC
 */
public class Attendance extends DefaultPanel {
    public static final String DATE_FORMAT = "MMM dd, yyyy hh:mm a";
    public static final String DEFAULT_STATUS = "Manage student's attendance";
    public final Logger logger = Logger.getLogger(Attendance.class.getName());
    private StatusBar statusBar;
    private DefaultTableModel scheduleModel;
    private DefaultTableModel studentModel;

    public void setStatusBar(StatusBar sb) {
        this.statusBar = sb;
    }

    /**
     * Creates new form Attendance
     */
    public Attendance() {
        initComponents();
        statusBar = new StatusBar();
        loadSchedulesToTable();
    }

    @Override
    public void refresh() {
        loadSchedulesToTable();
    }

    @Override
    public void doUpdate() {
        Map<Integer, Boolean> s_p = new HashMap<Integer, Boolean>();

        for (int i = 0; i < studentModel.getRowCount(); i++) {
            s_p.put((Integer) studentModel.getValueAt(i, 0),
                    (Boolean) studentModel.getValueAt(i, 3));
        }

        if (tblScheduleList.getSelectedRow() == -1) {
            statusBar.setMessage("No valid schedule has been selected for update.", StatusBar.WARNING_TYPE);
            return;
        }
        int scheduleId = (Integer) scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 0);
        try {
            new AttendanceDb().update(scheduleId, s_p);
            statusBar.setMessage("Update successful.", StatusBar.MESSAGE_TYPE);
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getSQLState());
            statusBar.setMessage("Update failed.", StatusBar.ERROR_TYPE);
        }

    }

    /**
     * Load student's current year attendance information
     */
    private void loadStudentInformation() {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        int present = 0;
        int total = 0;
        String yearStart = new StringBuilder()
                .append("01-01-").append(currentYear)
                .append(" 00:00").toString();
        String yearEnd = new StringBuilder()
                .append("12-31-").append(currentYear)
                .append(" 00:00").toString();
        int classId = (Integer) scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 1);
        int stdId = (Integer) studentModel.getValueAt(tblStudentList.getSelectedRow(), 0);
        String stdName = (String) studentModel.getValueAt(tblStudentList.getSelectedRow(), 2);
        try {
            for (ScheduleInfo si : new ScheduleDb().select(0, classId, 0, Helper.toDateTime(yearStart), Helper.toDateTime(yearEnd))) {
                for (Map.Entry<Integer, Boolean> e : new AttendanceDb().select(si.getId()).entrySet()) {
                    if (e.getKey() == stdId) {
                        total++;
                        if (e.getValue()) present++;
                    }
                }
            }

            lblStdId.setText("" + stdId);
            lblStdName.setText("" + stdName);
            if (total == 0) {
                lblTotal.setText("N/A");
                lblAttended.setText("N/A");
                lblRate.setText("N/A");
            } else {
                lblTotal.setText("" + total);
                lblAttended.setText("" + present);
                Double rate = (double) present / total * 100f;
                lblRate.setText("" + rate + " %");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Attendance.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Load all students when user choose a schedule
     */
    private void loadStudentsToTable() {
        studentModel = (DefaultTableModel) tblStudentList.getModel();
        studentModel.setRowCount(0);

        int classId = (Integer) scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 1);
        try {
            List<StudentInfo> stdList = new StudentDb().select("", classId);
            Map<Integer, Boolean> existList = new AttendanceDb()
                    .select((Integer) scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 0));

            for (StudentInfo s : stdList) {
                if (!s.isActive())
                    continue;

                boolean isExisted = false;

                for (Map.Entry<Integer, Boolean> e : existList.entrySet()) {
                    if (s.getId() == e.getKey()) {
                        studentModel.addRow(new Object[]{
                                s.getId(),
                                scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 2),
                                s.getName(),
                                e.getValue()
                        });
                        isExisted = true;
                        break;
                    }
                }

                if (!isExisted) {
                    studentModel.addRow(new Object[]{
                            s.getId(),
                            scheduleModel.getValueAt(tblScheduleList.getSelectedRow(), 2),
                            s.getName(),
                            false
                    });
                }
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Load all Schedules from database to table when initialize
     */
    private void loadSchedulesToTable() {
        scheduleModel = (DefaultTableModel) tblScheduleList.getModel();
        scheduleModel.setRowCount(0);
        List<ScheduleInfo> scheduleList = null;
        List<ScheduleInfo> activeSchedule = new ArrayList<ScheduleInfo>();

        JDatePicker datePicker = new JDatePicker();
        datePicker.putClientProperty("JDatePicker.TimePickerUI", SpinnerTimePickerUI.class.getName());
        datePicker.putClientProperty("JDatePicker.showTimePicker", Boolean.TRUE);
        datePicker.setEditable(true);
        datePicker.setDateFormat("MM/dd/yy hh:mm a");
        tblScheduleList.setDefaultEditor(java.util.Date.class, JDatePicker.createTableCellEditor(datePicker));
        tblScheduleList.setDefaultRenderer(java.util.Date.class, new DateTimeCellRenderer());

        try {
            scheduleList = new ScheduleDb().select();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, ex.getMessage());
            statusBar.setMessage("Unable to load data.", StatusBar.WARNING_TYPE);
        }

        if (scheduleList == null) {
            return;
        }
        for (ScheduleInfo si : scheduleList) {
            try {
                boolean isSubjectActive = new SubjectDb().select(si.getSubjectId()).isActive();
                boolean isUserActive = new UserDb().select(si.getUserId()).isActive();
                boolean isClassActive = new ClassDb().select(si.getClassId()).isActive();
                if (isSubjectActive && isUserActive && isClassActive) {
                    activeSchedule.add(si);
                }
            } catch (SQLException e) {
                logger.log(Level.SEVERE, null, e);
            }
        }

        addDataToScheduleTable(activeSchedule);
    }

    /**
     * Add a list of data to table schedule list
     *
     * @param result list of data to add
     */
    private void addDataToScheduleTable(List<ScheduleInfo> result) {
        try {
            for (ScheduleInfo s : result) {
                scheduleModel.addRow(new Object[]{
                        s.getId(),
                        s.getClassId(),
                        new ClassDb().select(s.getClassId()).getName(),
                        new SubjectDb().select(s.getSubjectId()).getName(),
                        new UserDb().select(s.getUserId()).getName(),
                        s.getStartTime(),
                        s.getEndTime()
                });
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getSQLState());
        }
    }

    /**
     * Search function
     */
    public void doSearch() {
        scheduleModel.setRowCount(0);
        List<ScheduleInfo> result;
        List<SubjectInfo> allSubjects = null;
        List<ClassInfo> allClasses = null;
        List<UserInfo> allUsers = null;

        String strStartTime = dpStart.getText();
        String strEndTime = dpEnd.getText();
        String subjectName = txtSubject.getText().trim();
        String userName = txtUser.getText().trim();
        String className = txtClass.getText().trim();
        int subjectId = 0;
        int userId = 0;
        int classId = 0;
        boolean foundSubject = subjectName.equals("");
        boolean foundUser = userName.equals("");
        boolean foundClass = className.equals("");

        try {
            allClasses = new ClassDb().select();
            allSubjects = new SubjectDb().select();
            allUsers = new UserDb().select();
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        }


        if (allSubjects != null) {
            for (SubjectInfo i : allSubjects) {
                if (!i.isActive()) {
                    continue;
                }
                if (i.getName().equalsIgnoreCase(subjectName)) {
                    subjectId = i.getId();
                    foundSubject = true;
                }
            }
        }

        if (allClasses != null) {
            for (ClassInfo i : allClasses) {
                if (!i.isActive()) {
                    continue;
                }
                if (i.getName().equalsIgnoreCase(className)) {
                    classId = i.getId();
                    foundClass = true;
                }
            }
        }

        if (allUsers != null) {
            for (UserInfo i : allUsers) {
                if (!i.isActive()) {
                    continue;
                }
                if (i.getName().equalsIgnoreCase(userName)) {
                    userId = i.getId();
                    foundUser = true;
                }
            }
        }

        if (!foundClass || !foundSubject || !foundUser) {
            statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            return;
        }

        Date startTime = (strStartTime.equals("")) ? null : Helper.toDate(strStartTime, DATE_FORMAT);
        Date endTime = (strEndTime.equals("")) ? null : Helper.toDate(strEndTime, DATE_FORMAT);

        try {
            result = new ScheduleDb().select(subjectId, classId, userId, startTime, endTime);

            if (result.isEmpty()) {
                statusBar.setMessage("No match found.", StatusBar.MESSAGE_TYPE);
            } else {
                addDataToScheduleTable(result);
                statusBar.setMessage(result.size() + " match(es) found.", StatusBar.MESSAGE_TYPE);
            }
        } catch (SQLException e) {
            logger.log(Level.WARNING, e.getSQLState());
            statusBar.setMessage("Search stopped.", StatusBar.ERROR_TYPE);
        }
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnUpdate = new javax.swing.JButton();
        pnScheduleList = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblStudentList = new javax.swing.JTable();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        lblStdId = new javax.swing.JLabel();
        lblStdName = new javax.swing.JLabel();
        lblTotal = new javax.swing.JLabel();
        lblAttended = new javax.swing.JLabel();
        lblRate = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnRefresh = new javax.swing.JButton();
        pnScheduleInfo = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblScheduleList = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        txtClass = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtSubject = new javax.swing.JTextField();
        txtUser = new javax.swing.JTextField();
        jLabel21 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        btnSearch = new javax.swing.JButton();
        dpStart = new com.standbysoft.component.date.swing.JDatePicker();
        dpStart.putClientProperty("JDatePicker.TimePickerUI", SpinnerTimePickerUI.class.getName());
        dpStart.putClientProperty("JDatePicker.showTimePicker", Boolean.TRUE);
        dpStart.setEmptySelectionText("<none>");

        dpEnd = new com.standbysoft.component.date.swing.JDatePicker();
        dpEnd.putClientProperty("JDatePicker.TimePickerUI", SpinnerTimePickerUI.class.getName());
        dpEnd.putClientProperty("JDatePicker.showTimePicker", Boolean.TRUE);
        dpEnd.setEmptySelectionText("<none>");

        jLabel8 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        btnUpdate.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/update-16.png"));
        btnUpdate.setText("Update");
        btnUpdate.setMaximumSize(new java.awt.Dimension(71, 23));
        btnUpdate.setMinimumSize(new java.awt.Dimension(71, 23));
        btnUpdate.setPreferredSize(new java.awt.Dimension(71, 23));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        pnScheduleList.setBackground(new java.awt.Color(255, 255, 255));
        pnScheduleList.setBorder(javax.swing.BorderFactory.createTitledBorder("Student list"));

        tblStudentList.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{

                },
                new String[]{
                        "Student ID", "Class", "Student name", "Present"
                }
        ) {
            Class[] types = new Class[]{
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean[]{
                    false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        tblStudentList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblStudentListMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tblStudentList);
        tblStudentList.getColumnModel().getColumn(2).setPreferredWidth(100);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Student information:");

        jLabel2.setText("- Student ID:");

        jLabel3.setText("- Student name:");

        jLabel5.setText("- Total schedules this year:");

        jLabel6.setText("- Schedules attended:");

        jLabel7.setText("- Rate:");

        javax.swing.GroupLayout pnScheduleListLayout = new javax.swing.GroupLayout(pnScheduleList);
        pnScheduleList.setLayout(pnScheduleListLayout);
        pnScheduleListLayout.setHorizontalGroup(
                pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnScheduleListLayout.createSequentialGroup()
                                .addGap(53, 53, 53)
                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel1)
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnScheduleListLayout.createSequentialGroup()
                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, pnScheduleListLayout.createSequentialGroup()
                                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                                        .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                                                .addGap(18, 18, 18)
                                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                        .addComponent(lblRate, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                                        .addComponent(lblTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                                        .addComponent(lblStdName, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                                        .addComponent(lblStdId, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)
                                                                        .addComponent(lblAttended, javax.swing.GroupLayout.DEFAULT_SIZE, 174, Short.MAX_VALUE)))
                                                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE))
                                                .addGap(32, 32, 32)))
                                .addContainerGap(46, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(pnScheduleListLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                                .addGap(20, 20, 20))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnScheduleListLayout.createSequentialGroup()
                                .addGap(35, 35, 35)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 340, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(78, Short.MAX_VALUE))
        );
        pnScheduleListLayout.setVerticalGroup(
                pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnScheduleListLayout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(pnScheduleListLayout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addGap(18, 18, 18)
                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel3)
                                                        .addComponent(lblStdName, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(18, 18, 18)
                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel5)
                                                        .addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(18, 18, 18)
                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel6)
                                                        .addComponent(lblAttended, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                .addGap(18, 18, 18)
                                                .addGroup(pnScheduleListLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel7)
                                                        .addComponent(lblRate, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addComponent(lblStdId, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addContainerGap(31, Short.MAX_VALUE))
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setForeground(new java.awt.Color(0, 102, 255));
        jLabel4.setText("MANAGE ATTENDANCES");

        btnRefresh.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/refresh-16.gif"));
        btnRefresh.setText("Refresh");
        btnRefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRefreshActionPerformed(evt);
            }
        });

        pnScheduleInfo.setBackground(new java.awt.Color(255, 255, 255));
        pnScheduleInfo.setBorder(javax.swing.BorderFactory.createTitledBorder("Schedule list"));
        pnScheduleInfo.setPreferredSize(new java.awt.Dimension(452, 219));

        tblScheduleList.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][]{

                },
                new String[]{
                        "ID", "Class ID", "Class", "Subject", "User", "Start time", "End time"
                }
        ) {
            Class[] types = new Class[]{
                    java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.util.Date.class, java.util.Date.class
            };
            boolean[] canEdit = new boolean[]{
                    false, true, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types[columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }
        });
        tblScheduleList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblScheduleListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblScheduleList);
        tblScheduleList.getColumnModel().getColumn(0).setPreferredWidth(30);
        tblScheduleList.getColumnModel().getColumn(5).setPreferredWidth(100);
        tblScheduleList.getColumnModel().getColumn(6).setPreferredWidth(100);

        javax.swing.GroupLayout pnScheduleInfoLayout = new javax.swing.GroupLayout(pnScheduleInfo);
        pnScheduleInfo.setLayout(pnScheduleInfoLayout);
        pnScheduleInfoLayout.setHorizontalGroup(
                pnScheduleInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnScheduleInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                                .addContainerGap())
        );
        pnScheduleInfoLayout.setVerticalGroup(
                pnScheduleInfoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnScheduleInfoLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(28, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(255, 255, 255));
        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Search for schedule"));
        jPanel4.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jPanel4.setMaximumSize(new java.awt.Dimension(452, 219));
        jPanel4.setMinimumSize(new java.awt.Dimension(452, 219));

        jLabel10.setText("Class:");

        jLabel21.setText("End time:");

        jLabel22.setText("User:");

        jLabel23.setText("Start time:");

        btnSearch.setIcon(new ImageIcon(Settings.IMAGES_DIR + "/search-16.png"));
        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        dpStart.setDateFormat(DATE_FORMAT);
        dpStart.setEditable(true);
        dpStart.setEmptySelectionText("<none>");

        dpEnd.setDateFormat(DATE_FORMAT);
        dpEnd.setEditable(true);
        dpEnd.setEmptySelectionText("<none>");

        jLabel8.setText("Subject:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel23)
                                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(jLabel22, javax.swing.GroupLayout.Alignment.TRAILING)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                        .addComponent(txtUser, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                                        .addComponent(txtClass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 167, Short.MAX_VALUE)
                                        .addComponent(dpStart, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel4Layout.createSequentialGroup()
                                                .addGap(58, 58, 58)
                                                .addComponent(btnSearch))
                                        .addGroup(jPanel4Layout.createSequentialGroup()
                                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel21)
                                                        .addComponent(jLabel8))
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(txtSubject)
                                                        .addComponent(dpEnd, javax.swing.GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE))))
                                .addGap(43, 43, 43))
        );
        jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                .addContainerGap(50, Short.MAX_VALUE)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(jLabel21)
                                                .addComponent(jLabel23))
                                        .addComponent(dpStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(dpEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel4Layout.createSequentialGroup()
                                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel10)
                                                        .addComponent(txtClass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(jLabel8))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel22)
                                                        .addComponent(txtUser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(jPanel4Layout.createSequentialGroup()
                                                .addComponent(txtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(btnSearch)))
                                .addGap(36, 36, 36))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(pnScheduleInfo, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(pnScheduleList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(461, 461, 461)
                                .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(405, Short.MAX_VALUE))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(jLabel4)
                                        .addContainerGap()))
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                                .addComponent(pnScheduleInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 257, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(11, 11, 11)
                                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 211, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addComponent(pnScheduleList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(btnRefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(23, 23, 23))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addGap(19, 19, 19)
                                        .addComponent(jLabel4)
                                        .addContainerGap(567, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        doUpdate();
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRefreshActionPerformed
        refresh();
    }//GEN-LAST:event_btnRefreshActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        doSearch();
    }//GEN-LAST:event_btnSearchActionPerformed

    private void tblScheduleListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblScheduleListMouseClicked
        loadStudentsToTable();
    }//GEN-LAST:event_tblScheduleListMouseClicked

    private void tblStudentListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblStudentListMouseClicked
        loadStudentInformation();
    }//GEN-LAST:event_tblStudentListMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private com.standbysoft.component.date.swing.JDatePicker dpEnd;
    private com.standbysoft.component.date.swing.JDatePicker dpStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblAttended;
    private javax.swing.JLabel lblRate;
    private javax.swing.JLabel lblStdId;
    private javax.swing.JLabel lblStdName;
    private javax.swing.JLabel lblTotal;
    private javax.swing.JPanel pnScheduleInfo;
    private javax.swing.JPanel pnScheduleList;
    private javax.swing.JTable tblScheduleList;
    private javax.swing.JTable tblStudentList;
    private javax.swing.JTextField txtClass;
    private javax.swing.JTextField txtSubject;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}

